package com.yuyi.wxt.controller;

import java.util.Date;
import java.util.Objects;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ConcurrentAccessException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yuyi.full.handler.exception.ResultBO;
import org.yuyi.full.handler.exception.ResultTool;

import com.alibaba.fastjson.JSONObject;
import com.yuyi.exception.WXTExceptionEnum;
import com.yuyi.model.ClientinfoDO;
import com.yuyi.model.KefuDO;
import com.yuyi.tool.JWTTool;
import com.yuyi.tool.RedisTool;
import com.yuyi.wxt.service.Clientinfo_WXT_Service;
import com.yuyi.wxt.service.Kefu_WXT_Service;

import redis.clients.jedis.Jedis;

@RestController
public class KefuLogin_WXT_Controller{
    
    @Autowired
    private Kefu_WXT_Service kefuService;
  
    @Autowired
    private Clientinfo_WXT_Service clientinfoService;
    
    /**
               * 登录
     * @param kefuNumber
     * @param kefuPassword
     * @return
     */
    @PostMapping("/kefuloging")
    public ResultBO<?> adminLogin(@RequestParam Integer kefuNumber,@RequestParam String kefuPassword){
        JSONObject json = new JSONObject();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(kefuNumber.toString(), kefuPassword);
        String userToken = JWTTool.sign(token.getPrincipal().toString(), token.getCredentials().toString()); 
        KefuDO kefu = kefuService.selectByKefuNumber(kefuNumber);
        try{
            subject.login(token);
            json.put("token", userToken);
            json.put("msg", kefu);
            Jedis num = RedisTool.getJedis15(); 
            // 获取所有key
            Set<String> number = num.keys("*");
            if(number.isEmpty()){
                num.set(kefu.getId().toString(), kefu.toString());
            }
            if(Objects.equals(number.contains(kefu.getId().toString()), false)) {
                num.set(kefu.getId().toString(), kefu.toString());
            }
            ClientinfoDO clientInfo = clientinfoService.selectByClientid(kefu.getKefuMail());
            Date Time;
//            if (clientInfo != null){
//              Time = new Date(System.currentTimeMillis());
//              clientInfo.setConnected(1);
//              clientInfo.setLastconnecteddate(Time);
//              clientinfoService.save(clientInfo);
//            }else{
//              Clientlnfo clientlnf = new Clientlnfo();
//              clientlnf.setClientid(String.valueOf(kefu.getKefuMail()));
//              Time = new Date(System.currentTimeMillis());       
//              clientlnf.setConnected(Short.valueOf((short)1));
//              clientlnf.setLastconnecteddate(Time);       
//              this.clientInfoRepository.save(clientlnf);
//            }
            return ResultTool.success(json);
        }
        catch (UnknownAccountException e){
            return ResultTool.error(WXTExceptionEnum.EXCEPTION_ADMINPASSWORD);
        }
        catch (IncorrectCredentialsException e){
            return ResultTool.error(WXTExceptionEnum.EXCEPTION_ADMINPASSWORD);
        }
        catch (LockedAccountException e){
            return ResultTool.error(WXTExceptionEnum.EXCEPTION_ADMINBEAR);
        }
        catch (ConcurrentAccessException  e) {
            return ResultTool.error(WXTExceptionEnum.EXCEPTION_ADMINNULL);
        }
    }
 
    /**
               * 退出
     * @return
     */
    @RequestMapping({"/kefulogout"})
    public ResultBO<?> kefuOut(@RequestParam(value="kefuNumber",required=false) Integer kefuNumber){
        Subject subject = SecurityUtils.getSubject();
        KefuDO kefu = (KefuDO) subject.getPrincipal();
        if(java.util.Objects.isNull(kefu)) {
            kefuService.updateStatus(kefuNumber);
            return ResultTool.success();
        }
        kefuService.updateStatus(kefu.getKefuNumber());
        subject.logout();     
        return ResultTool.success();
    }
  
}
